package com.petdog.module.recognition.controller;

import com.petdog.common.dto.ApiResponse;
import com.petdog.module.recognition.dto.BreedInfoDto;
import com.petdog.module.recognition.service.BreedInfoService;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 犬种科普信息控制器
 * 提供犬种信息的查询和科普内容展示API
 */
@RestController
@RequestMapping("/breed-info")
@RequiredArgsConstructor
public class BreedInfoController {

    private static final Logger log = LoggerFactory.getLogger(BreedInfoController.class);
    private final BreedInfoService breedInfoService;

    /**
     * 根据犬种ID获取犬种科普信息
     */
    @GetMapping("/{breedId}")
    public ApiResponse<BreedInfoDto> getBreedInfoById(@PathVariable Integer breedId) {
        log.info("获取犬种信息, breedId: {}", breedId);
        
        try {
            // 调用服务获取犬种信息
            com.petdog.infrastructure.entity.BreedInfo breedInfo = breedInfoService.getBreedById(breedId);
            
            if (breedInfo == null) {
                log.warn("未找到犬种信息, breedId: {}", breedId);
                return ApiResponse.error("未找到犬种信息");
            }
            
            // 转换为DTO
            BreedInfoDto dto = convertToDto(breedInfo);
            return ApiResponse.success(dto);
        } catch (Exception e) {
            log.error("获取犬种信息失败, breedId: {}", breedId, e);
            return ApiResponse.error("获取犬种信息失败: " + e.getMessage());
        }
    }

    /**
     * 根据犬种名称获取犬种科普信息
     */
    @GetMapping("/name")
    public ApiResponse<BreedInfoDto> getBreedInfoByName(@RequestParam String name) {
        log.info("获取犬种信息, 名称: {}", name);
        
        try {
            // 调用服务获取犬种信息
            com.petdog.infrastructure.entity.BreedInfo breedInfo = breedInfoService.getBreedByName(name);
            
            if (breedInfo == null) {
                log.warn("未找到犬种信息, 名称: {}", name);
                return ApiResponse.error("未找到犬种信息");
            }
            
            // 转换为DTO
            BreedInfoDto dto = convertToDto(breedInfo);
            return ApiResponse.success(dto);
        } catch (Exception e) {
            log.error("获取犬种信息失败, 名称: {}", name, e);
            return ApiResponse.error("获取犬种信息失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有犬种科普信息列表
     */
    @GetMapping("/all")
    public ApiResponse<List<BreedInfoDto>> getAllBreedInfo() {
        log.info("获取所有犬种信息列表");
        
        try {
            // 调用服务获取所有犬种信息
            List<com.petdog.infrastructure.entity.BreedInfo> breedInfos = breedInfoService.getAllBreeds();
            
            if (breedInfos == null || breedInfos.isEmpty()) {
                log.warn("未找到犬种信息");
                return ApiResponse.success(List.of());
            }
            
            // 转换为DTO列表
            List<BreedInfoDto> dtos = breedInfos.stream()
                    .map(this::convertToDto)
                    .collect(Collectors.toList());
            
            return ApiResponse.success(dtos);
        } catch (Exception e) {
            log.error("获取所有犬种信息失败", e);
            return ApiResponse.error("获取所有犬种信息失败: " + e.getMessage());
        }
    }

    /**
     * 将BreedInfo实体转换为BreedInfoDto
     */
    private BreedInfoDto convertToDto(com.petdog.infrastructure.entity.BreedInfo breedInfo) {
        if (breedInfo == null) {
            return null;
        }
        
        BreedInfoDto dto = new BreedInfoDto();
        dto.setBreedId(breedInfo.getBreedId());
        dto.setBreedName(breedInfo.getBreedName());
        dto.setScientificName(breedInfo.getScientificName());
        dto.setOriginCountry(breedInfo.getOriginCountry());
        dto.setSize(breedInfo.getSize());
        dto.setWeightRange(breedInfo.getWeightRange());
        dto.setHeightRange(breedInfo.getHeightRange());
        dto.setLifeExpectancy(breedInfo.getLifeExpectancy());
        dto.setPersonality(breedInfo.getPersonality());
        dto.setAppearance(breedInfo.getAppearance());
        dto.setGroomingNeeds(breedInfo.getGroomingNeeds());
        dto.setExerciseNeeds(breedInfo.getExerciseNeeds());
        dto.setHealthConcerns(breedInfo.getHealthConcerns());
        dto.setTrainability(breedInfo.getTrainability());
        dto.setSuitableFor(breedInfo.getSuitableFor());
        dto.setImageUrl(breedInfo.getImageUrl());
        dto.setDescription(breedInfo.getDescription());
        dto.setPopularityRank(breedInfo.getPopularityRank());
        
        return dto;
    }
}